package nxu.it.controller;

import io.jooby.*;
import io.jooby.annotation.*;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import nxu.it.model.ApiResult;
import nxu.it.model.LoginRequest;
import nxu.it.service.GreetingService;
import nxu.it.service.LoginService;
import security.LoginFilter;
import security.NeedLogin;

import java.time.LocalDateTime;
import java.util.*;

@Path("/")
@Singleton
public class MainController {
    public static final String LOGIN_SESSION_FIELD="username";
    GreetingService greetingService;
    LoginService loginService=new LoginService();


    @Inject
    public MainController(GreetingService greetingService) {

        this.greetingService = greetingService;
    }

    @GET(value = "/", produces = MediaType.HTML)
    @NeedLogin
    public String index() {
        return "<h1>Yes, it works!</h1>";
    }

    @GET(value = "/bye",produces = MediaType.HTML)
    public ModelAndView bye(@QueryParam String name, @QueryParam Integer gender){
        Map<String,Object>data=new HashMap<>();
        data.put("yonghu",name);
        data.put("age",18);
        data.put("gender",gender);
        List<Integer>numbers=List.of(1,2,3,4,5);
        data.put("numbers",numbers);
        ModelAndView<Object>mv=new ModelAndView<>("bye.flt",data);
        return mv;

    }
    @GET(value = "/hello", produces = MediaType.HTML)
    public String hello(@QueryParam String name,@QueryParam Integer gender) {
        return greetingService.sayHello(name, String.valueOf(gender));
    }

    @GET(value = "/json", produces = MediaType.JSON)
    @NeedLogin
    public ApiResult json() {

        return ApiResult.ok("请求成功");
    }

    @GET(value = "/render", produces = MediaType.HTML)
    @NeedLogin
    public ModelAndView render(Context ctx) {
        if (ctx.session(LOGIN_SESSION_FIELD).isMissing()){
            ctx.sendRedirect(StatusCode.TEMPORARY_REDIRECT,"/login");
        }
        return new ModelAndView("test.ftl", Map.of("name", "Jack"));
    }

    @GET(value = "/login",produces = MediaType.HTML)
    public ModelAndView login(Context ctx){
        Map<String,Object>data=new HashMap<>();
        Value username=ctx.session(LOGIN_SESSION_FIELD);
        ModelAndView mv;
        if (username.isMissing()){
            mv=new ModelAndView("login.ftl",data);
        }else {
            data.put("username",username.value());
            mv=new ModelAndView("welcome.ftl",data);
        }
        return mv;
    }
    @POST(value = "/loginCheck", produces = MediaType.HTML)
    public String loginCheck(Context ctx,@FormParam LoginRequest loginRequest){
        System.out.println(loginRequest);
        if (LoginService.loginCheck(loginRequest)) {
            ctx.session().put(LOGIN_SESSION_FIELD, loginRequest.getUsername());
            return "登录成功";
        } else {
            return "用户名或密码错误";
        }
    }
}
    /*public String loginCheck(@FormParam("username") String username, @FormParam("password") String password) {
        if(username == null || password == null) {
            return "用户名或密码不能为空！";
        }
        if(username.length()<6 || password.length()<6) {
            return "用户名或密码长度不能小于6！";
        }
        if(username.equals("baiduqin")&&password.equals("88888888"))
        return "登录成功";
        else return "用户名或密码错误";
    }
    @GET(value = "/register", produces = MediaType.HTML)
    public ModelAndView registerFtl(Context ctx) {
        List<String> hometown = Arrays.asList("宁夏","陕西","甘肃","其他");
        List<String> hobbies = Arrays.asList("唱歌","跳舞","篮球","说唱");
        Map<String, Object> model = new HashMap<>();
        model.put("hobbies", hobbies);
        model.put("provinces", hometown);
        return new ModelAndView("register.ftl", model);
    }
    @POST(value = "/doRegister", produces = MediaType.HTML)
    public String doRegister(
            @FormParam("username") String username,
            @FormParam("password") String password,
            @FormParam("gender") String gender,
            @FormParam("age") int age,
            @FormParam("province") String province,
            @FormParam("email") String email,
            @FormParam("hobbies") String hobbies) {
        return "我的用户名是"+username+",密码是"+password+",性别是"+gender+"," +
                "今年"+age+"岁,籍贯是"+province+",邮箱是"+email+",爱好是"+hobbies+"。";
    }
    @POST(value = "/today", produces = MediaType.JSON)
    public Map<String, String> getToday() {
        Map<String, String> dateInfo = new HashMap<>();
        LocalDateTime currentDateTime = LocalDateTime.now();
        dateInfo.put("weekday", String.format("%tA",currentDateTime));
        dateInfo.put("minute", String.format("%tM",currentDateTime));
        dateInfo.put("hour", String.format("%tH",currentDateTime));
        dateInfo.put("day", String.format("%te",currentDateTime));
        dateInfo.put("month", String.format("%tm",currentDateTime));
        dateInfo.put("year", String.format("%tY",currentDateTime));
        return dateInfo;
    }

}*/

